package com.bjpowernode.services.impls;

import com.bjpowernode.entities.TPermission;
import com.bjpowernode.entities.TUser;
import com.bjpowernode.mappers.TPermissionMapper;
import com.bjpowernode.mappers.TRoleMapper;
import com.bjpowernode.mappers.TUserMapper;
import com.bjpowernode.querys.BaseQuery;
import com.bjpowernode.querys.UserQuery;
import com.bjpowernode.services.UserService;
import com.bjpowernode.utils.LoginInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private TUserMapper tUserMapper;
    @Resource
    private TRoleMapper tRoleMapper;
    @Resource
    private TPermissionMapper tPermissionMapper;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser tUser = tUserMapper.selectByLoginAct(username);
        if (tUser == null)
            throw new UsernameNotFoundException("登录账号不存在");
        tUser.setTRoleList(tRoleMapper.selectByUserId(tUser.getId()));
        List<TPermission> tPermissionList = tPermissionMapper.selectByUserId(tUser.getId());
        tUser.setTPermissionList(tPermissionList);
        return tUser;
    }

    @Override
    public TUser getUserById(Integer id) {
        return tUserMapper.selectById(id);
    }

    @Override
    public PageInfo<TUser> getUsersByPage(Integer current) {
        PageHelper.startPage(current, 10);
        List<TUser> tUserList = tUserMapper.selectByPage(new BaseQuery());
        return new PageInfo<>(tUserList);
    }

    @Override
    public List<TUser> getOwners() {
        return tUserMapper.selectOwners();
    }

    @Override
    public int addUser(UserQuery userQuery) {
        TUser tUser = new TUser();
        BeanUtils.copyProperties(userQuery, tUser);
        tUser.setLoginPwd(passwordEncoder.encode(tUser.getLoginPwd()));
        tUser.setCreateTime(new Date());
        tUser.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());
        return tUserMapper.insert(tUser);
    }

    @Override
    public int updateUser(UserQuery userQuery) {
        TUser tUser = new TUser();
        BeanUtils.copyProperties(userQuery, tUser);
        if (StringUtils.hasText(userQuery.getLoginPwd2()))
            tUser.setLoginPwd(passwordEncoder.encode(userQuery.getLoginPwd2()));
        tUser.setEditTime(new Date());
        tUser.setEditBy(LoginInfoUtil.getCurrentLoginUser().getId());
        return tUserMapper.updateById(tUser);
    }

    @Override
    public int deleteUserById(Integer id) {
        return tUserMapper.deleteById(id);
    }

    @Override
    public int batchDeleteUsersByIds(List<String> idList) {
        return tUserMapper.batchDeleteByIds(idList);
    }
}
